package service.Impl;

import context.BaseContext;
import dao.Impl.OrderDaoImpl;
import dao.Impl.ShopCartDaoImpl;
import dao.Impl.ShopDaoImpl;
import dao.Impl.UserDaoImpl;
import dao.OrderDao;
import model.DTO.OrderPaymentDTO;
import model.VO.OrderBackVO;
import model.VO.OrderVO;
import model.entity.*;
import service.OrderService;
import util.NowTime;
import util.RandomNum;

import java.util.ArrayList;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    OrderDaoImpl orderDao =new OrderDaoImpl();
    ShopCartDaoImpl shopCartDao =new ShopCartDaoImpl();
    UserDaoImpl userDao=new UserDaoImpl();
    //取消订单
    @Override
    public void cancel(int id, String cancelReason) {
        Order order=orderDao.getById(id);
        int payStatus=0;
        if(order.getStatus()==3){
            payStatus=1;
        }
        orderDao.cancel(id,cancelReason,payStatus);
    }

    //完成订单
    @Override
    public void complete(int id) {
        orderDao.complete(id);
    }
    //创建订单
    @Override
    public OrderBackVO order(float amount, int payMethod, String remark,int type) {
        User user=userDao.getUserById(BaseContext.getCurrentId());
        ShopDaoImpl shopDao = new ShopDaoImpl();
        Shop shop=shopDao.getShop();
        String number= orderDao.order(amount,payMethod,remark,type,user.getPhone(),user.getName(),shop.getShop_address());
        Order o= orderDao.getByNumber(number);
        OrderBackVO vo=new OrderBackVO(o.getId(),o.getAmount(),o.getNumber(),o.getOrder_time());
        List<ShoppingCart> Cart= shopCartDao.Get(BaseContext.getCurrentId());
        for(ShoppingCart c:Cart){
            orderDao.orderDetail(c.getDish_id(),c.getSetmeal_id(),c.getCategory_id(),c.getName(),c.getImage(),o.getId(),c.getNumber(),c.getAmount());
            shopCartDao.deleteById(c.getId());
        }

        return vo;
    }

    //后台获取订单列表
    @Override
    public List<Order> page(int page, int pageSize, String status, String number, String orderTime) {
        return orderDao.pageQuery(page,pageSize,status,number,orderTime);
    }

    //查询订单详情
    @Override
    public OrderVO getInfo(int id) {
        return orderDao.getOrderInfo(id);
    }

    //修改订单状态为请取餐
    @Override
    public void pickMeal(int id) {
        orderDao.pickMeal(id);
    }
    //订单支付
    @Override
    public void payment(OrderPaymentDTO orderPaymentDTO) {
        Order order = new Order();
        order.setNumber(orderPaymentDTO.getOrderNumber());
        order.setPay_method(orderPaymentDTO.getPayMethod());
        order.setPay_status(1);
        order.setCheckout_time(new NowTime().getTime());
        order.setStatus(3);
        order.setTakeout_number(new RandomNum().number());
        orderDao.payment(order);
    }
    //查询历史订单
    @Override
    public List<OrderVO> list(String status) {
        return orderDao.list(status);
    }
    //再下一单
    @Override
    public void repetiton(int id) {
        OrderVO orderVO=orderDao.getOrderInfo(id);
        List<OrderDetail> orderDetailList=orderVO.getOrderDetailList();
        for(OrderDetail orderDetail:orderDetailList){
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setName(orderDetail.getName());
            shoppingCart.setImage(orderDetail.getImage());
            shoppingCart.setUser_id(BaseContext.getCurrentId());
            shoppingCart.setDish_id(orderDetail.getDish_id());
            shoppingCart.setSetmeal_id(orderDetail.getSetmeal_id());
            shoppingCart.setCategory_id(orderDetail.getCategory_id());
            shoppingCart.setNumber(orderDetail.getNumber());
            shoppingCart.setAmount(orderDetail.getAmount());
            shoppingCart.setCategory_id(orderDetail.getCategory_id());
            shoppingCart.setCreate_time(new NowTime().getTime());
            shopCartDao.insert(shoppingCart);
        }
    }

    //商家是否同意退款
    @Override
    public void refund(int id,int isAgree) {
        String shopReply;
        int status;
        int payStatus;
        if(isAgree==1){
            shopReply="同意退款";
            status=7;
            payStatus=2;
        }else {
            shopReply="商品马上制作完成，无法退款";
            status=3;
            payStatus=1;
        }
        orderDao.refund(id,status,payStatus,shopReply);
    }
}
